package pro.seac.twitter.domain;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@MappedSuperclass
public class BaseEntity {
	static final String SEQUENCE = "SIMPLER_TWITTER_SEQENCE";
	@Id
	@SequenceGenerator(allocationSize = 100, sequenceName = SEQUENCE, name = SEQUENCE)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SEQUENCE)
	protected Integer id;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false)
	protected Date createdDate;

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable = false)
	protected Date modifiedDate;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Date getCreatedDate() {
		return createdDate;
	}

	public Date getModifiedDate() {
		return modifiedDate;
	}

	public void setCreatedDate(Date createdDate) {
		this.createdDate = createdDate;
	}

	public void setModifiedDate(Date modifiedDate) {
		this.modifiedDate = modifiedDate;
	}

	public void beforePersist() {
		createdDate = modifiedDate = currentDate();
	}

	public void beforeUpdate() {
		modifiedDate = currentDate();
	}

	protected Date currentDate() {
		return new Date();
	}
}
